﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Emmajn.ChildParentExtension.SilverlightClassLibrary
{
    /// <summary>
    /// Represents a telephone number.
    /// </summary>
    public class TelephoneNumber
    {
        private string countryCode;
        private string areaCode;
        private string cityCode;
        private string number;
        private string extension;

        /// <summary>
        /// Initializes a new instance of the TelephoneNumber class.
        /// </summary>
        public TelephoneNumber(string areaCode, string cityCode, string number, string countryCode = null, string extension = null)
        {
            AreaCode = areaCode;
            CityCode = cityCode;
            Number = number;
            CountryCode = countryCode;
            Extension = extension;
        }

        /// <summary>
        /// [Country code (up to 3 digits)]
        /// </summary>
        public string CountryCode
        {
            get
            {
                return countryCode;
            }
            set
            {
                Assert.IsValidOptionalString(value, 3);
                countryCode = value;
            }
        }

        /// <summary>
        /// Area code (up to 3 digits)
        /// </summary>
        public string AreaCode
        {
            get
            {
                return areaCode;
            }
            set
            {
                Assert.IsValidRequiredString(value, 3);
                areaCode = value;
            }
        }

        /// <summary>
        /// City code (up to 4 digits)
        /// </summary>
        public string CityCode
        {
            get
            {
                return cityCode;
            }
            set
            {
                Assert.IsValidRequiredString(value, 4);
                cityCode = value;
            }
        }

        /// <summary>
        /// VisitorNumber (up to 4 digits)
        /// </summary>
        public string Number
        {
            get
            {
                return number;
            }
            set
            {
                Assert.IsValidRequiredString(value, 4);
                number = value;
            }
        }

        /// <summary>
        /// [Extension number (up to 6 digits)]
        /// </summary>
        public string Extension
        {
            get
            {
                return extension;
            }
            set
            {
                Assert.IsValidOptionalString(value, 6);
                extension = value;
            }
        }

        /// <summary>
        /// Returns a string that represents the current object.
        /// </summary>
        public override string ToString()
        {
            StringBuilder stringBuilder = new StringBuilder();
            if (countryCode != null) stringBuilder.AppendFormat("+{0} ", countryCode);
            stringBuilder.AppendFormat("{0}-{1}-{2}", areaCode, cityCode, number);
            if (extension != null) stringBuilder.AppendFormat(" Ext {0}", extension);
            return stringBuilder.ToString();
        }
    }
}
